<?php namespace Admin\Controller; 

class GoodsController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\Goods;
	}
	public function index(){
		$data=$this->model
			 ->join("goods_detail","gid","=","goods_gid")
			 ->get();
//			 p($data);
//			 exit;
		View::with('data',$data);
		View::make();
	}
	
    //添加
    public function add(){
		if(IS_POST){

			if($this->model->store()){
				View::success('添加成功',U('index'));
			}
			View::error($this->model->getError());
			
		}
		$cateModel=new \Common\Model\Cate;
		$cate=$cateModel->get();
		//将分类名称以树状的形式显示出来
		$cateData=Data::tree($cate,'cname');
		View::with('cateData',$cateData);
		View::make();
    }
	  public function upload(){
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}
	  public function getAttr(){
		//得到类型id
		$tid = Q('post.t',0,'intval');
		//通过类型id找到类型属性
		$typeAttrModel = new \Common\Model\TypeAttr;
		$data = $typeAttrModel->where("type_tid={$tid}")->get();
		foreach ($data as $k => $v) {
			//把字符串变为数组，将来好循环
			$data[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
//		p($data);
		echo json_encode($data);exit;
	}
	
	public function del(){
		$gid=Q('get.gid',0,'intval');
		$this->model->where("gid={$gid}")->delete();
		View::success('删除成功');
	}
	
	public function edit(){
		//修改
		if(IS_POST){
//			P(Q('post.'));die;
			if($this->model->edit()){
				View::success('修改成功',U('index'));
			}
			View::error($this->model->getError());
		}
		//获取旧数据
		$gid=Q('get.gid',0,'intval');
		$oldData=$this->model
			 ->join("goods_detail","gid","=","goods_gid")
			 ->where("gid={$gid}")
			 ->find();
		p($oldData);
		View::with('oldData',$oldData);
		//处理所属分类数据
		$cateModel=new \Common\Model\Cate;
		$cate=$cateModel->get();
		$cateData=Data::tree($cate,'cname');
//		p($cateData);
		//处理商品属性规格
		$goodsAttrModel=new \Common\Model\GoodsAttr;
		//商品属性和类型属性关联
		$data=$goodsAttrModel
			 ->join('type_attr','type_attr_taid','=','taid')
			 ->where("goods_gid={$gid}")
			 ->get();
		$taid=[];
		foreach ($data as $k => $v) {
			$data[$k]['tavalue']=explode(',', $v['tavalue']);
			$taid[]=$v['taid'];
		}
//		p($taid);
		//获取没有选择的属性
		$typeAttrModel=new \Common\Model\TypeAttr;
		$taidStr=implode(',', $taid);
//		p($taidStr);
		$typeAttr=$typeAttrModel
				 ->where("taid NOT IN ({$taidStr}) AND type_tid={$oldData['type_tid']}")
				 ->get();
//		p($typeAttr);		 
		foreach ($typeAttr as $v) {
			$data[]=array(
				'gavalue'=>'',
				'taname'=>$v['taname'],
                'tavalue'=>explode(',',$v['tavalue']),
                'class'=>$v['class']
			);
			
		}
//		p($data);
		
		View::with('data',$data);
		View::with('cateData',$cateData);
		View::make();
	}
	
}
